//
//  SponsorInfoCell.swift
//  Employee
//
//  Created by swordjoy on 2017/3/28.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class SponsorInfoCell: UITableViewCell {
  static var identifier = "SponsorInfoCell.swordjoy"
  
  static func height(with titles: [String]) -> CGFloat {
    var height: CGFloat = 0
    for title in titles { height += (UIFont.calculateHeight(width: UIScreen.width - 40, font: GlobalConfig.system15Font, text: title) + 5) }
    return height + 60
  }
  
  var model: OrderDetailModel? {
    
    didSet {
      guard let temp = model else { return }
      
      nameLabel.text = temp.launcherName
      phoneNumberLabel.text = temp.launcherMobile
      
      hotLayoutLabels(with: temp.houseAddress)
    }
  }
  
  fileprivate var sponsorLabel: UILabel!
  fileprivate var nameLabel: UILabel!
  fileprivate var phoneNumberLabel: UILabel!
  fileprivate var addressLabels: [UILabel] = []
  fileprivate var margin: CGFloat = 50
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    selectionStyle = .none
    backgroundColor = UIColor.white
    
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - fileprivate

extension SponsorInfoCell {
  
  fileprivate func hotLayoutLabels(with titles: [String]) {
    
    for button in addressLabels {
      if let _ = button.superview { button.removeFromSuperview() }
    }
    // 因为重用所以要重新布局
    addressLabels.removeAll()
    
    var cacheY: CGFloat = 0
    
    for title in titles {
      
      let height = UIFont.calculateHeight(width: UIScreen.width - 40, font: GlobalConfig.system15Font, text: title) + 5
      let y = margin + cacheY
      
      let label = UILabel(frame: CGRect(x: 20, y:  y, width: UIScreen.width - 40, height: height))
      label.text = title
      label.textColor = GlobalConfig.TextDeepColor
      label.numberOfLines = 0
      label.font = GlobalConfig.system15Font
      
      cacheY += height
      
      addressLabels.append(label)
      addSubview(label)
    }
    
  }
  
  fileprivate func create() {
    sponsorLabel = UILabel()
    addSubview(sponsorLabel)
    sponsorLabel.font = GlobalConfig.system12Font
    sponsorLabel.textColor = GlobalConfig.TextLightColor
    sponsorLabel.text = "发起人"
    sponsorLabel.snp.makeConstraints { (make) in
      make.left.equalTo(20)
      make.top.equalToSuperview()
    }
    
    nameLabel = UILabel()
    addSubview(nameLabel)
    nameLabel.font = GlobalConfig.system16Font
    nameLabel.textColor = GlobalConfig.TextDeepColor
    nameLabel.snp.makeConstraints { (make) in
      make.left.equalTo(20)
      make.top.equalTo(sponsorLabel.snp.bottom).offset(10)
    }
    
    phoneNumberLabel = UILabel()
    addSubview(phoneNumberLabel)
    phoneNumberLabel.font = GlobalConfig.system16Font
    phoneNumberLabel.textColor = GlobalConfig.TextDeepColor
    phoneNumberLabel.snp.makeConstraints { (make) in
      make.left.equalTo(nameLabel.snp.right).offset(14)
      make.top.equalTo(nameLabel.snp.top)
    }
  }
}
